﻿using System.IO;
using UnityEditor;
using UnityEngine;

public class FixHill
{
    [MenuItem("GTATools/GamePlay/SandBox/MapTools/FixHill")]
    public static void Do()
    {
        string hillMaterialPath = "Assets/Arts/World/sandtable/materials/mat_sandtable_hill02.mat";
        var hillMaterial = AssetDatabase.LoadAssetAtPath<Material>(hillMaterialPath);

        string colliderMaterialPath = "Assets/Arts/Scene/TATestScenes/Transparent/Materials/TA_Transparent_Forbidden_Area.mat";
        var colliderMaterial = AssetDatabase.LoadAssetAtPath<Material>(colliderMaterialPath);

        string directory = "Assets/ResourcesAssets/World/prefabs/sandtable/mountains/";
        var files = Directory.GetFiles(directory, "*_hill*.prefab");

        foreach (var file in files)
        {
            var contentsRoot = PrefabUtility.LoadPrefabContents(file);
            var meshRenders = contentsRoot.GetComponentsInChildren<MeshRenderer>(true);
            foreach (var meshRenderer in meshRenders)
            {
                if (meshRenderer.name.EndsWith("_col"))
                {
                    // 隐藏的碰撞模型，赋予碰撞的材质
                    if(meshRenderer.sharedMaterial != null && !meshRenderer.sharedMaterial.name.Contains("lambert1"))
                        UnityEngine.Debug.LogWarningFormat(contentsRoot, "{0}'s material is {1}", meshRenderer.name, meshRenderer.sharedMaterial);
                    meshRenderer.sharedMaterial = colliderMaterial;
                }
                else
                {
                    // 山体给与统一的材质
                    if (meshRenderer.sharedMaterial != null && !meshRenderer.sharedMaterial.name.Contains("mat_sandtable_hill02"))
                        UnityEngine.Debug.LogWarningFormat(contentsRoot, "{0}'s material is {1}", meshRenderer.name, meshRenderer.sharedMaterial);
                    meshRenderer.sharedMaterial = hillMaterial;
                }
            }

            PrefabUtility.SaveAsPrefabAsset(contentsRoot, file);
            PrefabUtility.UnloadPrefabContents(contentsRoot);
        }
    }
}
